package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.DictionaryVerifyAdapter;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DictionaryVerifyAdapterImpl implements DictionaryVerifyAdapter {

    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;

    @Override
    public void verifyDictionary(String group, String verifyCode, FunctionException exception) {

        if (StringUtils.isBlank(group)) {
            throw new FunctionException("OC-01-001-01-15-001", "group");
        }
        if (StringUtils.isBlank(verifyCode)) {
            throw new FunctionException("OC-01-001-01-15-001", "verifyCode");
        }
        if (exception == null) {
            throw new FunctionException("OC-01-001-01-15-001", "exception");
        }
        List<DictionaryResponse> dictionaryResponseList = baseDataServiceAdapter.queryDictionarys(group);

        if (CollectionUtils.isEmpty(dictionaryResponseList)) {
            throw exception;
        }
        for (DictionaryResponse dictionaryResponse : dictionaryResponseList) {
            if (dictionaryResponse.getCode().equals(verifyCode)) {
                return;
            }
        }
        throw exception;
    }
}
